package com.homework.Homework07;

public class Homework07 {
    public static void main(String[] args) {

        Student student = new Student("jack", '男', 18, "20230001");
        student.printInfo();
        Teacher teacher = new Teacher("lisa", '女', 36, 11);
        teacher.printInfo();

        //定义多态数组，里面保存2个学生和2个教师，要求按年龄从高到低排序
        Person[] persons = new Person[4];
        persons[0] = new Student("jack",'男',15,"0001");
        persons[1] = new Student("smith",'男',17,"0002");
        persons[2] = new Teacher("lisa",'女',36,11);
        persons[3] = new Teacher("lucky",'女',30,3);

        //创建对象
        Homework07 homework07 = new Homework07();
        homework07.bubbleSort(persons);

        //输出排序后的数组
        System.out.println("------排序后的数组------");
        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i]);

        }

        //遍历数组，调用test方法
        System.out.println("-----------------------");
        for (int i = 0; i < persons.length; i++) {
            homework07.test(persons[i]);
        }

    }

    //定义方法，形参为Person类型，功能：调用学生的study或教师的teach方法
    //分析这里会使用到向下转型和类型判断
    public void test(Person p){
        if(p instanceof Student){
            ((Student) p).Study();
        }else if(p instanceof Teacher){
            ((Teacher) p).Teach();
        }else{
            System.out.println("do nothing");
        }
    }


    //方法，完成年龄从高到低排序
    public void bubbleSort(Person[] persons){
        Person temp = null;
        for (int i = 0; i < persons.length - 1; i++) {
            for (int j = 0; j < persons.length - 1 - i; j++) {
                if(persons[j].getAge() < persons[j + 1].getAge()) {
                    temp = persons[j];
                    persons[j] = persons[j + 1];
                    persons[j + 1] = temp;
                }
            }
        }
    }

}
